package com.fyqiang.web;

import com.fyqiang.api.APIResponse;
import com.fyqiang.entity.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/10.
 */
@RestController
@RequestMapping("/user")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public APIResponse<?> login(HttpServletRequest request, UserInfo userInfoReq) {
        logger.info("address===" + request.getRemoteAddr() + "==userInfoReq==" + userInfoReq.toString());
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername(userInfoReq.getUsername());
        userInfo.setUserid(110);
        if (userInfoReq.getUsername().equals("admin") && userInfoReq.getPwd().equals("123456")) {
            return APIResponse.returnSuccess(userInfo);
        }
        return APIResponse.returnFail(userInfo, "用户名或密码错误");
    }

    @RequestMapping(value = "/validcode", method = RequestMethod.POST)
    public APIResponse<?> validcode(HttpServletRequest request) {
        logger.info("address===" + request.getRemoteAddr() + "==username==" + request.getParameterMap().toString());

        return APIResponse.returnSuccess("http://www.xctcf.com/member/common/verify?t=" + System.currentTimeMillis());
    }

    List<UserInfo> userInfoList = new ArrayList<UserInfo>();

    @RequestMapping(value = "/usermanagerlist", method = RequestMethod.POST)
    public APIResponse<?> usermanagerlist(HttpServletRequest request) {
        logger.info("address===" + request.getRemoteAddr() + "==username==" + request.getParameterMap().toString());
        if (userInfoList.isEmpty()) {
            for (int i = 0; i < 20; i++) {
                UserInfo userInfo = new UserInfo();
                userInfo.setUserid(i);
                userInfo.setUsername("吴境丽是不是傻");
                userInfo.setDepartment("研发部门");
                userInfo.setRealname("吴境丽");
                userInfo.setPhonenum("110");
                userInfo.setPwd("******************");
                userInfoList.add(userInfo);
            }
        }
        return APIResponse.returnSuccess(userInfoList);
    }

    @RequestMapping(value = "/namagerUserAdd", method = RequestMethod.POST)
    public APIResponse<?> namagerUserAdd(HttpServletRequest request, UserInfo userInfo) {
        userInfoList.add(userInfo);
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value = "/namagerUserModify", method = RequestMethod.POST)
    public APIResponse<?> namagerUserModify(HttpServletRequest request, UserInfo userInfo) {
        for (UserInfo infoTemp : userInfoList) {
            if (infoTemp.getUserid().intValue() == userInfo.getUserid().intValue()) {
                infoTemp.setRealname(userInfo.getRealname());
                infoTemp.setPwd(userInfo.getPwd());
                infoTemp.setUsername(userInfo.getUsername());
                infoTemp.setPhonenum(userInfo.getPhonenum());
                infoTemp.setDepartment(userInfo.getDepartment());
            }
        }
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value = "/namagerUserDelete", method = RequestMethod.POST)
    public APIResponse<?> namagerUserDelete(HttpServletRequest request, UserInfo userInfo) {
        for (int i = 0; i < userInfoList.size(); i++) {
            if (userInfoList.get(i).getUserid().intValue() == userInfo.getUserid().intValue()) {
                userInfoList.remove(i);
            }
        }
        return APIResponse.returnSuccess();
    }
}
